#include "../include/gudb.h"
#include "../src/parser/sql_parser.h"
#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("Debugging CREATE INDEX parser step by step...\n");
    
    const char* sql = "CREATE INDEX idx_test ON users (id)";
    printf("SQL: %s\n", sql);
    
    // Create parser
    parser_t* parser = parser_create(sql);
    if (!parser) {
        printf("Failed to create parser\n");
        return 1;
    }
    
    printf("Initial parser state:\n");
    printf("  current_token type: %d\n", parser->current_token ? parser->current_token->type : -1);
    printf("  current_token value: %s\n", parser->current_token && parser->current_token->value ? parser->current_token->value : "NULL");
    
    // Manually step through the CREATE INDEX parsing logic
    printf("\n=== Stepping through CREATE INDEX parsing ===\n");
    
    // Step 1: Check current token should be CREATE
    if (parser->current_token && parser->current_token->type == TOKEN_CREATE) {
        printf("✓ Found CREATE token: %s\n", parser->current_token->value);
        
        // Consume CREATE token
        token_destroy(parser->current_token);
        parser->current_token = lexer_next_token(parser->lexer);
        
        printf("After consuming CREATE, current_token: type=%d, value=%s\n", 
               parser->current_token ? parser->current_token->type : -1,
               parser->current_token && parser->current_token->value ? parser->current_token->value : "NULL");
        
        // Step 2: Check for INDEX token
        if (parser->current_token && parser->current_token->type == TOKEN_INDEX) {
            printf("✓ Found INDEX token: %s\n", parser->current_token->value);
            
            // Consume INDEX token
            token_destroy(parser->current_token);
            parser->current_token = lexer_next_token(parser->lexer);
            
            printf("After consuming INDEX, current_token: type=%d, value=%s\n", 
                   parser->current_token ? parser->current_token->type : -1,
                   parser->current_token && parser->current_token->value ? parser->current_token->value : "NULL");
            
            // Step 3: Check for index name (identifier)
            if (parser->current_token && parser->current_token->type == TOKEN_IDENTIFIER) {
                printf("✓ Found index name: %s\n", parser->current_token->value);
                
                // Consume index name
                token_destroy(parser->current_token);
                parser->current_token = lexer_next_token(parser->lexer);
                
                printf("After consuming index name, current_token: type=%d, value=%s\n", 
                       parser->current_token ? parser->current_token->type : -1,
                       parser->current_token && parser->current_token->value ? parser->current_token->value : "NULL");
                
                // Step 4: Check for ON token
                if (parser->current_token && parser->current_token->type == TOKEN_ON) {
                    printf("✓ Found ON token: %s\n", parser->current_token->value);
                    printf("SUCCESS: All tokens parsed correctly!\n");
                } else {
                    printf("✗ Expected ON token but found: type=%d, value=%s\n", 
                           parser->current_token ? parser->current_token->type : -1,
                           parser->current_token && parser->current_token->value ? parser->current_token->value : "NULL");
                }
            } else {
                printf("✗ Expected identifier for index name but found: type=%d, value=%s\n", 
                       parser->current_token ? parser->current_token->type : -1,
                       parser->current_token && parser->current_token->value ? parser->current_token->value : "NULL");
            }
        } else {
            printf("✗ Expected INDEX token but found: type=%d, value=%s\n", 
                   parser->current_token ? parser->current_token->type : -1,
                   parser->current_token && parser->current_token->value ? parser->current_token->value : "NULL");
        }
    } else {
        printf("✗ Expected CREATE token but found: type=%d, value=%s\n", 
               parser->current_token ? parser->current_token->type : -1,
               parser->current_token && parser->current_token->value ? parser->current_token->value : "NULL");
    }
    
    // Clean up
    parser_destroy(parser);
    
    return 0;
}